<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2019-04-09
 * Time: 14:07
 */

namespace app\mobile\controller;

use app\common\model\QuActive;
use app\common\model\QuAnswer;
use app\common\model\QuClasses;
use app\common\model\QuQuestion;
use app\common\model\QuStudent;

class Index extends Base
{
    public function index()
    {
        if (request()->isAjax()) {
            $name = input('name', '');
            $num = input('num', '');
            $cid = input('cid', 0);
            $aid = input('aid', 1);
            if (!$name) {
                return returnJson(202, '请输入姓名');
            }
            if (!$num) {
                return returnJson(202, '请输入学号');
            }

            $student = db('qu_student')
                ->where(['name'=>$name,'num'=>$num])
                ->find();
            if(!$student){
                return returnJson(202, '档案信息不存在，请核对姓名以及学号，信息正确则请联系任课老师');
            }

//            if (!$cid) {
//                return returnJson(202, '请选择班级');
//            }
            $data = [
                'name' => $name,
                'num' => $num,
                'update_time' => time(),
                'create_time' => time(),
                'ip' => isset($_SERVER["REMOTE_ADDR"]) ? $_SERVER["REMOTE_ADDR"] : ''
            ];

            $student = QuStudent::getOne(['num' => $num]);
            if (!$student) {
                $res = QuStudent::setGetLasetId($data);
                $sid = $res;
            } else {
                if (empty($student['ip'])) {
                    $ip = isset($_SERVER["REMOTE_ADDR"]) ? $_SERVER["REMOTE_ADDR"] : '';
                    if ($ip) {
                        QuStudent::update(['ip' => $ip], ['id' => $student['id']]);
                    }
                }
                $answer = QuAnswer::getOne(['sid' => $student['sid'], 'aid' => $aid]);
                if ($answer) {
                    return returnJson(202, '您已参与过此次课堂测验');
                } else {
                    $sid = $student['id'];
                }

            }
            return returnJson(200, '', ['aid' => input('aid', 1), 'sid' => $sid]);
        }
        $classes = QuClasses::getAll([]);
        return view('index', compact('classes'));
    }

    public function answer()
    {
        if (!input('sid', 0)) {
            $this->redirect('index');
        }

        $answer1 = QuAnswer::getOne(['sid' => input('sid'), 'aid' => input('aid')]);
        if ($answer1) {
            return view('', ['msg' => '您已参与过此次课堂测验']);
        }

        if (request()->isPost()) {
            $ajax = input('');
            $score = 0;

            if (!empty($ajax['tiankong'])) {

                foreach ($ajax['tiankong'] as $value) {
                    $question = QuQuestion::getOne(['id' => $value['qid']]);
                    $answer = $question['answer1'];
                        if ($value['answer'] == $answer) {
                            $score += $question['score'];
                        }
                }
            }

            if (!empty($ajax['panduan'])) {

                foreach ($ajax['panduan'] as $value) {
                    $question = QuQuestion::getOne(['id' => $value['qid']]);
                    $answer = json_decode($question['answer'], true);
                    foreach ($answer as $val) {
                        if ($val['is_right'] && ($val['content'] == $value['answer'])) {
                            $score += $question['score'];
                            break;
                        }
                    }
                }
            }
            if (!empty($ajax['danxuan'])) {

                foreach ($ajax['danxuan'] as $value) {
                    $question = QuQuestion::getOne(['id' => $value['qid']]);
                    $answer = json_decode($question['answer'], true);
                    foreach ($answer as $val) {
                        if ($val['is_right'] && ($val['content'] == $value['answer'])) {
                            $score += $question['score'];
                            break;
                        }
                    }
                }
            }
            if (!empty($ajax['duoxuan'])) {
                foreach ($ajax['duoxuan'] as $value) {
                    $question = QuQuestion::getOne(['id' => $value['qid']]);
                    $answer = json_decode($question['answer'], true);
                    $sign = true;
                    foreach ($answer as $val) {
                        if ($val['is_right'] && (!in_array($val['content'], $value['answer']))) {
                            $sign = false;
                        }
                    }
                    if ($sign) {
                        $score += $question['score'];
                    }
                }
            }
            $save_data = [
                'score' => $score > 100 ? 100 : $score,
                'aid' => $ajax['aid'],
                'sid' => $ajax['sid'],
                'update_time' => time(),
                'create_time' => time(),
                'answer' => json_encode($ajax)
            ];

            QuAnswer::create($save_data);
            QuActive::setFinc(['id' => $ajax['aid']], 'people', 1);
            return returnJson(200, '', $score);
        }

        $aid = input('aid', 1);
        $active = QuActive::getOne(['id' => $aid]);
        $list = QuQuestion::getAll(['aid' => $aid], 'type asc,id asc');
        $num = range('A', 'Z');
        $q1 = $q2 = $q3 = $q4 = $q5 = [];
        if ($list) {
            foreach ($list as $key => $value) {
                $right = [];
                if($value['type']!=4){
                    $answer = json_decode($value['answer'], true);
                    if($answer){
                        foreach ($answer as $k => $val) {
                            $answer[$k]['sign'] = $num[$k];
                            if ($val['is_right']) {
                                $right[] = $num[$k];
                            }
                        }
                    }
                    $list[$key]['answer_arr'] = $answer;
                    $list[$key]['answer'] = json_decode($value['answer'], true);
                }
                switch ($value['type']) {
                    case 1:
                        $q1[] = $list[$key];
                        break;
                    case 2:
                        $q2[] = $list[$key];
                        break;
                    case 3:
                        $q3[] = $list[$key];
                        break;
                    case 4:
                        $q4[] = $list[$key];
                        break;
                    case 5:
                        $q5[] = $list[$key];
                        break;
                }
            }
        }
        $q1_size = sizeof($q1);
        $q2_size = sizeof($q2);
        $q3_size = sizeof($q3);
        $q4_size = sizeof($q4);
        $q5_size = sizeof($q5);
        $count=0;
        if($q1_size){
            $count++;
        }
        if($q2_size){
            $count++;
        }
        if($q3_size){
            $count++;
        }
        if($q4_size){
            $count++;
        }
        if($q5_size){
            $count++;
        }

        return view('', compact('q1', 'q2', 'q3', 'q4','q5', 'active', 'q1_size', 'q2_size','q3_size','q4_size','q5_size','count'));
    }

    public function score()
    {
        return view('');
    }
}